package cn.com.klun.tjy.other.tests;

import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

// 有序数组元素去重
public class Solution {

    public static void main(String[] args) {
        int[] numbers= new int[100];
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
            numbers[i] = random.nextInt(200);
        }
        int length1 = numbers.length;
        System.out.println("去重前长度："+length1);

        int[] ints = removeDuplicates(numbers);
        int length = ints.length;
        System.out.println();
        System.out.println("去重完成剩余"+length+"长度");
        for (int i = 0; i < length; i++) {
            System.out.println("第"+i+"个值为："+ints[i]);
        }
    }
    private static int[] removeDuplicates(int[] array){
        // 利用 treeSet 去重排序
        Set<Integer> sets = new TreeSet<>();
        int length = array.length;
        for (int i = 0; i < length; i++) {
            System.out.println("原生的数组"+array[i]+",");
            sets.add(array[i]);
        }
        int setsLen = sets.size();
        int[] result = new int[setsLen];
        int i = 0;
        Iterator<Integer> iterator = sets.iterator();
        while (iterator.hasNext()){
            Integer next = iterator.next();
            System.out.println("*********");
            result[i++] = next;
        }
        return result;
    }
}
